/**
 * @Title: UserController.java
 * @Package com.lemon.controller.cityRun
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年10月17日
 * @version V1.0
 */
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrUsersInfo;
import com.lemon.service.cityRun.CrSysRuleService;
import com.lemon.service.cityRun.CrUserService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;

/**
 * <p>
 * Title: UserController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年10月17日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/cruser")
public class CrUsersController {

	@Resource
	private CrUserService userService;
	@Resource
	private CrSysRuleService ruleService;

	//获取全部用户信息
	@RequestMapping("/getUsersList")
	public ReqResult getUsersList(@ModelAttribute CrUsersInfo user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
			//查询客户列表
			String string = request.getParameter("searName");
			if (StringUtil.isNotBlank(string)) {
				user.setWhere(
						"  and a.status != 0  and  a.wx_open_id like concat('%',?,'%') or a.nick_name like concat('%',?,'%')" ,
						string ,
						string);
			} else {
				user.setWhere("  and a.status != 0 ");
			}
			user.setOrderBy(" order by a.create_time desc");
			List<CrUsersInfo> list = userService.selectListBySelective(user);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询微信用户异常");
		}
		return reqResult;
	}


	/**
	 * <b>Description:<blockquote> 新增管理员信息
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/updateUsersInfo.do" , method = RequestMethod.POST)
	public ReqResult updateUsersInfo(@ModelAttribute CrUsersInfo user , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String ids = request.getParameter("remAllIds");

			if (StringUtil.isNotBlank(ids)) {
				CrUsersInfo u2 = new CrUsersInfo();
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					String id = split[i];
					u2.setStatus(0);
					u2.setId(id);
					userService.updateUserInfoById(u2);
				}
				reqResult.setReturnResult("1");
				return reqResult;
			}
			int row = userService.updateUserInfoById(user);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

}
